package com.weilan.ark3.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weilan.ark3.entity.BasicConfigDto;
import com.weilan.ark3.entity.ControlRtu;
import com.weilan.ark3.entity.page.PageQuery;
import com.weilan.ark3.entity.page.TableDataInfo;
import com.weilan.ark3.mapper.TbSearchMapper;
import com.weilan.ark3.service.TbSearchService;
import com.weilan.ark3.sqlite.ActSqliteUtil;
import com.weilan.ark3.sqlite.SQLiteUtilsFault;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据查询
 */
@Service
public class TbSearchServiceImpl implements TbSearchService {
    @Autowired
    private TbSearchMapper tbSearchMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public List<Object> listByTb(List<String> tbNameList, String deviceId, String startTime, String endTime) {
        List<Object> list = tbSearchMapper.listByTb(tbNameList, deviceId, startTime, endTime);

/*        for (Object o : list) {
            HashMap<String, String> map = (HashMap<String, String>) o;
            map.entrySet().removeIf(entry -> entry.getKey().startsWith("reserve"));
        }*/
        return list;
    }

    @Override

    public TableDataInfo<Object> listByModuleName(List<String> tbNameList,
                                                  String deviceId,
                                                  String slaveId,
                                                  String startTime,
                                                  String endTime,
                                                  PageQuery pageQuery) {

        Page<Object> page = tbSearchMapper.listByModuleName(tbNameList, deviceId,slaveId, startTime, endTime, pageQuery.build());
        List<Object> records = page.getRecords();

        for (Object o : records) {
            Map<String, String> map = (HashMap<String, String>) o;
            //替换时间
//            map.entrySet().removeIf(entry -> entry.getKey().startsWith("预留"));

            //去除多余元素
            map.entrySet().removeIf(entry -> entry.getKey().startsWith("Reserved"));
            map.entrySet().removeIf(entry -> entry.getKey().startsWith("Year"));
            map.entrySet().removeIf(entry -> entry.getKey().startsWith("Month"));
            map.entrySet().removeIf(entry -> entry.getKey().startsWith("Day"));
            map.entrySet().removeIf(entry -> entry.getKey().startsWith("Hour"));
            map.entrySet().removeIf(entry -> entry.getKey().startsWith("Minute"));
            map.entrySet().removeIf(entry -> entry.getKey().startsWith("Second"));
        }
        return TableDataInfo.build(page);
    }

    @Override
    public int selectPvId() {

        return tbSearchMapper.selectPvId();
    }

    /**
     * 故障查询
     */
    @Override
    public TableDataInfo<Object> listByBreakdown(String startTime, String endTime) {
        return SQLiteUtilsFault.selectToDB2(startTime, endTime);
    }

    @Override
    public TableDataInfo<Object> listByact(String startTime, String endTime) {

        return ActSqliteUtil.selectToDBAct(startTime, endTime);
    }

    @Override
    public String findTable(String name) {
        return tbSearchMapper.findTable(name);
    }

    @Override
    public boolean saveConfig(ControlRtu controlRtu) {
        return tbSearchMapper.insertDevice(controlRtu);
    }
    @Override
    public List<ControlRtu> findAllmessge(String name, Object num){
        return tbSearchMapper.findAllmessge(name,num);
    }

    @Override
    public boolean deletecontrortu(){
        return tbSearchMapper.deletecontrortu();
    }
    @Override
    public boolean deletebasicconfig(){
        return tbSearchMapper.deletebasicconfig();
    }
    @Override
    public List<BasicConfigDto> selectAllbasic(){
        return tbSearchMapper.selectAllbasic();
    }

    @Override
    public void insertAllbasic(BasicConfigDto basicConfigDto){
     tbSearchMapper.insertAllbasic(basicConfigDto);
    }

    @Override
    public List<Map<String, Object>> selectSocPower(List<String> nameList,String date){
        return tbSearchMapper.selectSocPower(nameList,date);
    }

}
